<%
total_traded = 0
game = Game.find(:first, :conditions=>["gencode=?",@gencode])
game_setting = game.game_setting
total_traded = Account.sum(:quantity, :conditions=>["game_id=? AND trade_year=?", game.id, game.current_year])
players = Player.find(:all, :conditions=>["game_id=? AND player_type=?", game.id, "A"])
total_supply = 0
players.each do |p|
  pb = PlayerBalance.find(:first, :conditions=>["player_id=? AND year=?", p.id, game.current_year])
  total_supply += pb.inventory_unit
end
%>
<table width="100%">
<tr>
  <td width="50%">
    <table width="100%">
      <tr align="center">
        <td width="20%"><a href="/user"><img src="/images/home_icon_small.png" border="0" style="vertical-align:middle;"/></a><br/>Home</td>
        <td width="20%"><a href="/game_runnings/gamepanel_a?gencode=<%=@gencode%>"><img src="/images/monitor_icon.png" border="0" style="vertical-align:middle;"/></a><br/>Panel A</td>
        <td width="20%"><a href="/game_runnings/gamepanel?gencode=<%=@gencode%>"><img src="/images/monitor_icon.png" border="0" style="vertical-align:middle;"/></a><br/>Panel B</td>
        <td width="20%"><a href="/game_runnings/summary?gencode=<%=@gencode%>"><img src="/images/summary_icon.jpg" border="0" style="vertical-align:middle;"/></a><br/>Summary</td>
        <td><a href="/game_runnings/setting_parameter?gencode=<%=@gencode%>"><img src="/images/inventory_icon.jpg" border="0" style="vertical-align:middle;"/></a><br/>Setting</td>
      </tr>
    </table>
  </td>
  <td width="2%">&nbsp;</td>
  <td>
    <span style="background-color: green; width:350px; font-size: 2em; color:white; vertical-align:middle;">
    Year: <span id="yearpanel"><%=@game.current_year%></span>
    &nbsp;&nbsp;&nbsp;
    Round: <span id="roundpanel">-</span>
    </span>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <%if @game.a_run_flag=='N'%>
    <img src="/images/stop_icon.jpg" width="50px" style="vertical-align:middle;"/>
    <%else%>
    <img src="/images/start_icon.gif" width="50px" style="vertical-align:middle;"/>
    <%end%>
  </td>
</tr>
</table>
<table width="100%">
<tr align="center">
  <td>
    <input type="button" value="1.Check" onclick="PopupCenter('/game_runnings/a_check/<%=@gencode%>','check_a',950,700);"/>
    <form action="/game_runnings/start_year_a/<%=@gencode%>" method="post"><input type="submit" value="2.Open"/></form>
  </td>
  <td><form action="/game_runnings/match_order/<%=@gencode%>" method="post"><input type="submit" value="3.Execute"/></form></td>
  <td width="80%" align="left">
    <span style="font-size:20px;">Supply (Unit) : ?</span>
    <span style="font-size:50px; color:white;"><%=number_with_delimiter(total_supply)%></span>
    &nbsp;&nbsp;&nbsp;
    <span style="font-size:20px;">Traded (Unit) : </span>
    <span style="font-size:50px; color:blue;"><%=number_with_delimiter(total_traded)%></span>
  </td>
</tr>
</table>
<div id="chartarea">
<table width="100%">
<tr align="center">
  <td>
    <img src="/images/search.gif" border="0" onclick="PopupCenter('/game_runnings/zoom_chart_data_sold/<%=@gencode%>','chartPop1',950,700);"/>
    <span id="chartarea1">
      <div id="chartdiv1" align="center">Chart will load here</div>
      <script type="text/javascript">
        var chart = new FusionCharts("/charts/Pie2D.swf", "ChartId", "400", "300", "0", "0");
        chart.setXMLUrl('/game_runnings/chart_data_sold/<%=@gencode%>');
        chart.render("chartdiv1");
      </script>
    </span>
  </td>
  <td>
    <img src="/images/search.gif" border="0" onclick="PopupCenter('/game_runnings/zoom_chart_data_bought/<%=@gencode%>','chartPop2',950,700);"/>
    <span id="chartarea2">
      <div id="chartdiv2" align="center">Chart will load here</div>
      <script type="text/javascript">
        var chart = new FusionCharts("/charts/Pie2D.swf", "ChartId", "400", "300", "0", "0");
        chart.setXMLUrl('/game_runnings/chart_data_bought/<%=@gencode%>');
        chart.render("chartdiv2");
      </script>
    </span>
  </td>
</tr>
</table>
</div>
<div id="order_area">
<%
  bids = Trade.find(:all, :conditions=>["bidask_flag=? AND game_id=? AND t_status=?", "B", game.id, "Openned"])
  asks = Trade.find(:all, :conditions=>["bidask_flag=? AND game_id=? AND t_status=?", "A", game.id, "Openned"])
%>
  <table width="100%">
    <tr>
      <td width="50%" valign="top" align="center">
        <table width="95%" style="border: 3px solid black; font-size: 18px;">
          <tr align="center" style="border: 3px solid black;" bgcolor="#cccccc">
            <th>Player</th><th>Price</th><th>Credit</th><th>Quantity</th>
          </tr>
          <%asks.each do |a|%>
          <tr align="center">
            <td align="left"><%=a.player.name%></td>
            <td><%if game_setting.show_ask_price=='N'%>?<%else%><%=a.price%><%end%></td>
            <td><%if game_setting.show_ask_credit_term=='N'%>?<%else%><%=a.credit_term%><%end%></td>
            <td><%if game_setting.show_ask_quantity=='N'%>?<%else%><%=number_with_delimiter(a.quantity)%><%end%></td>
          </tr>
          <%end%>
        </table>
      </td>
      <td valign="top" align="center">
        <table width="95%" style="border: 3px solid black; font-size: 18px;">
          <tr align="center" style="border: 3px solid black;" bgcolor="#cccccc">
            <th>Player</th><th>Price<=</th><th>Credit>=</th><th>Quantity</th>
          </tr>
          <%bids.each do |b|%>
          <tr align="center">
            <td align="left"><%=b.player.name%></td>
            <td><%if game_setting.show_bid_price=='N'%>?<%else%><%=b.price%><%end%></td>
            <td><%if game_setting.show_bid_credit_term=='N'%>?<%else%><%=b.credit_term%><%end%></td>
            <td><%if game_setting.show_bid_quantity=='N'%>?<%else%><%=number_with_delimiter(b.quantity)%><%end%></td>
          </tr>
          <%end%>
        </table>
      </td>
    </tr>
  </table>
</div>
<script type="text/javascript">
  function refresh_order(){
    new Ajax.Updater('order_area', '/game_runnings/order_area/<%=@gencode%>', {asynchronous:true, evalScripts:true});
  }
  <%if @game.a_run_flag=="Y"%>
    window.setInterval(refresh_order,5000);
  <%end%>
</script>